package com.it.myh.read;

import com.it.myh.query.probability.TimeZone;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class ReadPltFile {

    public static List<LocationAndTime> locationAndTimes = new ArrayList<>();

    public static void main(String[] args) {
        String filePath = "C:\\Users\\IoV321\\Desktop\\test";
        List<LocationAndTime> locationAndTimes = readPltFiles(filePath);
//        System.out.println(countAll);//总记录数为：24876978
        System.out.println(locationAndTimes.size());//测试数据为：1564363
    }

    //递归文件夹
    public static List<LocationAndTime> readPltFiles(String folderPath) {
        File folder = new File(folderPath);

        // 检查文件夹是否存在
        if (folder.exists() && folder.isDirectory()) {
            File[] files = folder.listFiles();

            // 遍历文件夹中的文件和子文件夹
            for (File file : files) {
                if (file.isFile() && file.getName().endsWith(".plt")) {
                    // 处理".plt"文件
                    processPltFile(file);
                } else if (file.isDirectory()) {
                    // 递归遍历子文件夹
                    readPltFiles(file.getAbsolutePath());
                }
            }
        }
        return locationAndTimes;
    }

    private static void processPltFile(File file){
        int startLine = 7; // 指定开始读取的行数
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line;//读取到的内容
            int lineCount = 1; // 当前行数计数器

            // 读取文件的每一行
            while ((line = reader.readLine()) != null) {
                if (lineCount >= startLine) {
                    // 开始处理每一行数据
                    processLine(line);
                }
                lineCount++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private static void processLine(String line) {
        // 在这里编写处理每一行数据的逻辑：取出每一行的纬经度，年月日时分秒
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        String[] split = line.split(",");
        LocationAndTime allLocationAndTime = new LocationAndTime();
        allLocationAndTime.setLatitude(Double.parseDouble(split[0]));
        allLocationAndTime.setLongitude(Double.parseDouble(split[1]));
        allLocationAndTime.setTime(LocalTime.parse(split[6], dateTimeFormatter));
        allLocationAndTime.setNum(1);
        //获取时间，来计算该时间是属于哪个时区的
        TimeZone timeZone = TimeZone.whichZone(allLocationAndTime.getTime());
        timeZone.setNum(0);
        allLocationAndTime.setTimeZone(timeZone);
        //将所有数据存储到集合当中
        locationAndTimes.add(allLocationAndTime);
/*        //对数据进行统计
            if (LocationAndTime.isSameLocation(allLocationAndTime,new LocationAndTime(39.984702,116.318417,null)) && TimeZone.isBelongZone(allLocationAndTime.getTime(),TimeZone.ZONE1)){
                count ++;  //数据有150个
            }*/
    }
}


